import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mall/entity/sub_category_entity.dart';
import 'package:mall/event/category_event.dart';
import 'package:mall/service/category_service.dart';
import 'package:mall/utils/SizeHelper.dart';
import 'package:mall/utils/navigator_util.dart';

class SubCategoryView extends StatefulWidget {
  @override
  _SubCategoryViewState createState() => _SubCategoryViewState();
}

class _SubCategoryViewState extends State<SubCategoryView> {
  CategoryService categoryService = CategoryService();
  List<SubCategoryEntity> subCategoryEntitys = List();

  var categoryName, categoryImage, categoryId;
  bool flag = true;

  @override
  void initState() {
    super.initState();
    print("SubCategoryView_initState");
  }

  _listener() {
    eventBus
        .on<CategoryEvent>()
        .listen((CategoryEvent event) => _updateView(event));
  }

  _updateView(CategoryEvent categoryEvent) {
    if (flag) {
      flag = false;
      print("_updateView");
      setState(() {
        categoryName = categoryEvent.categoryName;
        categoryImage = categoryEvent.categoryImage;
        categoryId = categoryEvent.id;
      });
      _getSubCategory(categoryEvent.id);
    }
  }



  _getSubCategory(int id) {
    var params = {"id": id};
    print(params);
    print("_getSubCategory");
    categoryService.getSubCategoryData(params, (subCategoryEntityList) {
      flag = true;
      setState(() {
        subCategoryEntitys = subCategoryEntityList;
      });
    }, onFail: (value) {});
  }

  @override
  Widget build(BuildContext context) {
    _listener();
    SizeFit.init();
    return Container(
      child: SingleChildScrollView(
        child: ListView(
          physics: NeverScrollableScrollPhysics(),
          padding: EdgeInsets.zero,
          shrinkWrap: true,
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(5.0.w),
              height: 200.w,
              child: Image.network(
                categoryImage ?? "",
                fit: BoxFit.fill,
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 0.0),
            ),
            Center(
              child: Text(
                categoryName ?? "",
                style: TextStyle(fontSize: 14.0, color: Colors.black54),
              ),
            ),
            MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: Container(
                  margin: EdgeInsets.only(left: 20, right: 20),
                  child: GridView.builder(
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: subCategoryEntitys.length,
                      shrinkWrap: true,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 3,
                          mainAxisSpacing: 2.0,
                          childAspectRatio: 0.85,
                          //垂直单个子Widget之间间距
                          crossAxisSpacing: 0.2),
                      itemBuilder: (BuildContext context, int index) {
                        return getItemView(subCategoryEntitys[index]);
                      }),
                )),
            // Padding(
            //   padding: EdgeInsets.only(top: 1.0),
            // ),
            // SizedBox(height: 200)
          ],
        ),
      ),
    );
  }

  _itemClick(int id) {
//    Navigator.push(
//      context,
//      new MaterialPageRoute(builder: (context) {
//        return new GoodsList(1008002);
//      }),
//    );
    NavigatorUtils.goCategoryGoodsListPage(context, categoryName, id);
  }

  Widget getItemView(SubCategoryEntity categoryName) {
    return GestureDetector(
      child: Card(
        elevation: 1.0,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            AspectRatio(
                //设置宽高比
                aspectRatio: 1.2 / 1.0,
                child: Image.network(
                  categoryName.picUrl,
                  fit: BoxFit.fill,
                  height: 70,
                  width: SizeFit.physicalWidth / 3.75,
                )),
            Padding(
              padding: EdgeInsets.only(top: 1.0),
            ),
            Text(
              categoryName.name,
              style: TextStyle(fontSize: 14.0, color: Colors.black54),
            ),
          ],
        ),
      ),
      onTap: () => _itemClick(categoryName.id),
    );
  }
}
